const user = require("../../database/user");
const qs = require("querystring");

module.exports = async (req, res, next)=>{
  const {tel, uId, uName, age, sex, power, tId} = req.body;

  if(req.power !== 0 && req.uId !== uId){
    res.send({
      code:4,
      msg:"权限不够"
    })
    return;
  }

  if(!uId){
    res.send({
      code:3,
      msg:"缺少必传参数：uId"
    })
    return;
  }

  // 将可能被修改的数据，打包
  const newData = {};
  if( tel )   newData.tel = tel;
  if( uName ) newData.uName = qs.unescape(uName);
  if( age )   newData.age = age-0;
  if( sex )   newData.sex = sex;
  if( power ) newData.power = power;
  if( tId )   newData.tId = tId;

  await user.updateOne({uId}, newData);

  // 如果修改的信息中，存在职位修改，将来还需要修改部门信息
  // await team.updateOne({tId}, {teamLeader:uId})

  res.send({
    code: 1,
    msg:  "修改成功"
  });
}